﻿#include "stdafx.h"
#include "Models_scene.h"

char *Models_scene::m_object1Names[Models_scene_object1_count];

Models_scene::Models_scene()
{
    m_boundingBoxMax = glm::vec3(0.0f);
    m_boundingBoxMin = glm::vec3(0.0f);
	LoadBoundBox();
}


Models_scene::~Models_scene()
{
}


//Object1文件夹的bmd文件名-勇者大陆
void Models_scene::InitMapObject1Name()
{
	static bool finit;
	if (finit) return;
	finit = true;
	memset(m_object1Names, 0, sizeof(m_object1Names));

	m_object1Names[0] = "Tree01.bmd";
	m_object1Names[1] = "Tree02.bmd";
	m_object1Names[2] = "Tree03.bmd";
	m_object1Names[3] = "Tree04.bmd";
	m_object1Names[4] = "Tree05.bmd";
	m_object1Names[5] = "Tree06.bmd";
	m_object1Names[6] = "Tree07.bmd";
	m_object1Names[7] = "Tree08.bmd";
	m_object1Names[8] = "Tree09.bmd";
	m_object1Names[9] = "Tree10.bmd";
	m_object1Names[10] = "Tree11.bmd";
	m_object1Names[11] = "Tree12.bmd";
	m_object1Names[12] = "Tree13.bmd";

	m_object1Names[20] = "Grass01.bmd";
	m_object1Names[21] = "Grass02.bmd";
	m_object1Names[22] = "Grass03.bmd";
	m_object1Names[23] = "Grass04.bmd";
	m_object1Names[24] = "Grass05.bmd";
	m_object1Names[25] = "Grass06.bmd";
	m_object1Names[26] = "Grass07.bmd";
	m_object1Names[27] = "Grass08.bmd";

	m_object1Names[30] = "Stone01.bmd";
	m_object1Names[31] = "Stone02.bmd";
	m_object1Names[32] = "Stone03.bmd";
	m_object1Names[33] = "Stone04.bmd";
	m_object1Names[34] = "Stone05.bmd";

	m_object1Names[40] = "StoneStatue01.bmd";
	m_object1Names[41] = "StoneStatue02.bmd";
	m_object1Names[42] = "StoneStatue03.bmd";

	m_object1Names[43] = "SteelStatue01.bmd";
	m_object1Names[44] = "Tomb01.bmd";
	m_object1Names[45] = "Tomb02.bmd";
	m_object1Names[46] = "Tomb03.bmd";

	m_object1Names[50] = "FireLight01.bmd";
	m_object1Names[51] = "FireLight02.bmd";

	m_object1Names[52] = "Bonfire01.bmd";

	m_object1Names[55] = "DoungeonGate01.bmd";

	m_object1Names[56] = "MerchantAnimal01.bmd";
	m_object1Names[57] = "MerchantAnimal02.bmd";
	m_object1Names[58] = "TreasureDrum01.bmd";
	m_object1Names[59] = "TreasureChest01.bmd";

	m_object1Names[60] = "Ship01.bmd";

	m_object1Names[65] = "SteelWall01.bmd";
	m_object1Names[66] = "SteelWall02.bmd";
	m_object1Names[67] = "SteelWall03.bmd";
	m_object1Names[68] = "SteelDoor01.bmd";

	m_object1Names[69] = "StoneWall01.bmd";
	m_object1Names[70] = "StoneWall02.bmd";
	m_object1Names[71] = "StoneWall03.bmd";
	m_object1Names[72] = "StoneWall04.bmd";
	m_object1Names[73] = "StoneWall05.bmd";
	m_object1Names[74] = "StoneWall06.bmd";

	m_object1Names[75] = "StoneMuWall01.bmd";
	m_object1Names[76] = "StoneMuWall02.bmd";
	m_object1Names[77] = "StoneMuWall03.bmd";
	m_object1Names[78] = "StoneMuWall04.bmd";

	m_object1Names[80] = "Bridge01.bmd";

	m_object1Names[81] = "Fence01.bmd";
	m_object1Names[82] = "Fence02.bmd";
	m_object1Names[83] = "Fence03.bmd";
	m_object1Names[84] = "Fence04.bmd";

	m_object1Names[85] = "BridgeStone01.bmd";

	m_object1Names[90] = "StreetLight01.bmd";

	m_object1Names[91] = "Cannon01.bmd";
	m_object1Names[92] = "Cannon02.bmd";
	m_object1Names[93] = "Cannon03.bmd";

	m_object1Names[95] = "Curtain01.bmd";

	m_object1Names[96] = "Sign01.bmd";
	m_object1Names[97] = "Sign02.bmd";

	m_object1Names[98] = "Carriage01.bmd";
	m_object1Names[99] = "Carriage02.bmd";
	m_object1Names[100] = "Carriage03.bmd";
	m_object1Names[101] = "Carriage04.bmd";

	m_object1Names[102] = "Straw01.bmd";
	m_object1Names[103] = "Straw02.bmd";

	m_object1Names[105] = "Waterspout01.bmd";
	m_object1Names[106] = "Well01.bmd";
	m_object1Names[107] = "Well02.bmd";
	m_object1Names[108] = "Well03.bmd";
	m_object1Names[109] = "Well04.bmd";

	m_object1Names[110] = "Hanging01.bmd";

	m_object1Names[111] = "Stair01.bmd";

	m_object1Names[115] = "House01.bmd";
	m_object1Names[116] = "House02.bmd";
	m_object1Names[117] = "House03.bmd";
	m_object1Names[118] = "House04.bmd";
	m_object1Names[119] = "House05.bmd";

	m_object1Names[120] = "Tent01.bmd";

	m_object1Names[121] = "HouseWall01.bmd";
	m_object1Names[122] = "HouseWall02.bmd";
	m_object1Names[123] = "HouseWall03.bmd";
	m_object1Names[124] = "HouseWall04.bmd";
	m_object1Names[125] = "HouseWall05.bmd";	//酒吧屋顶角
	m_object1Names[126] = "HouseWall06.bmd";	//酒吧屋顶边

	m_object1Names[127] = "HouseEtc01.bmd";
	m_object1Names[128] = "HouseEtc02.bmd";
	m_object1Names[129] = "HouseEtc03.bmd";

	m_object1Names[130] = "Light01.bmd";
	m_object1Names[131] = "Light02.bmd";
	m_object1Names[132] = "Light03.bmd";

	m_object1Names[133] = "PoseBox01.bmd";	//等待拾取对象，无需绘制

	m_object1Names[140] = "Furniture01.bmd";
	m_object1Names[141] = "Furniture02.bmd";
	m_object1Names[142] = "Furniture03.bmd";
	m_object1Names[143] = "Furniture04.bmd";
	m_object1Names[144] = "Furniture05.bmd";
	m_object1Names[145] = "Furniture06.bmd";
	m_object1Names[146] = "Furniture07.bmd";

	m_object1Names[150] = "Candle01.bmd";
	m_object1Names[151] = "Beer01.bmd";
	m_object1Names[152] = "Beer02.bmd";
	m_object1Names[153] = "Beer03.bmd";
}

void Models_scene::LoadBoundBox()
{
	m_textureMgr.SetLoadDir("./Data/Object1/");
	Model mdl;
	bool fok = mdl.Load("./Data/Object1/PoseBox01.bmd", &m_textureMgr);
	if (fok)
	{
		m_boundingBoxMax = mdl.m_boundingBoxMax;
		m_boundingBoxMin = mdl.m_boundingBoxMin;
	}
	else assert(0);
}

void Models_scene::LoadObject1(char *objDir)
{
	InitMapObject1Name();
	Model *mdl = 0;
	int dirLen = strlen(objDir);
	int count = sizeof(m_object1Names) / sizeof(char*);
	char path[MAX_PATH];

	strcpy(path, objDir);
	m_models.clear();
	m_models.resize(count);
	for (int i = 0; i<count; i++)
	{
		if (i == Models_scene_object1_PoseBox01_index) continue;
		if (m_object1Names[i] == 0)
		{
			m_models[i] = 0;
			continue;
		}
		strcpy(path + dirLen, m_object1Names[i]);
		mdl = new Model();
		if (mdl->Load(path, &m_textureMgr))
		{
			m_models[i] = mdl;
		}
		else delete mdl;
	}
}

void Models_scene::Load(int index)
{
	Model *mdl = 0;
	char szdir[300];
	int dirLen = 0;
	m_textureMgr.SetLoadDir("./Data/Item/");
	m_textureMgr.GetTexture("bottle.OZT");
	m_textureMgr.GetTexture("pot3.OZT");
	m_textureMgr.GetTexture("apple.OZJ");
	dirLen = sprintf(szdir, "./data/object%d/", ++index);
	m_textureMgr.SetLoadDir(szdir);
	if (index == 1)
	{
		LoadObject1(szdir);
	}
	else
	{
		int i = 1;
		while (1)
		{
			if (index == 3 && (i == 25 || i == 27 || i == 33 || i == 90))
			{//3号地图有些模型是空的
				m_models.push_back(0);
				i++;
				continue;
			}
			sprintf(szdir + dirLen, "object%02d.bmd", i++);
			mdl = new Model();
			if (mdl->Load(szdir, &m_textureMgr))
			{
				m_models.push_back(mdl);
			}
			else
			{
				delete mdl;
				break;
			}
		}
		//模型索引不知为何多一个，只好加个凑数
		m_models.push_back(0);
	}
}
